/* * 
 * File: LoginView.java
 * @author Adam Phung
 * Email: adam.phung@gmail.com
 * 
 * Date Created: Oct 3, 2012
 * Date Modified: Oct 30, 2012 
 */

package edu.bu.cs673.superkids123.common.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.AbstractView;
import edu.bu.cs673.superkids123.children.model.Theme;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.common.controller.LoginController;
import edu.bu.cs673.superkids123.common.model.Child;
import edu.bu.cs673.superkids123.parents.view.CreateChildView;
import edu.bu.cs673.superkids123.parents.view.CreateParentView;

/**
 * The Class LoginView.
 * Displays a grid of buttons representing child user accounts.  Single click to login as selected child user.
 */
public class LoginView extends AbstractNavigationView implements ActionListener {

	/** Main content panel. */
	MenuPanel panel = new MenuPanel(new BorderLayout(H_GAP, V_GAP));
	private JScrollPane scrollPane = new JScrollPane();
	private JPanel mainPanel = new JPanel();

	private static int H_GAP = 20;
	private static int V_GAP = H_GAP;
	private static int ACTIVITY_COLS = 3;
	private static float AVATAR_SCALE = 0.75f;
	private static final int SCROLL_SPEED = 16;
	private static final int MIN_BUTTONS = 6;

	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();

	// private static JLabel directions = new
	// JLabel("Click on your name to continue");

	/**
	 * Instantiates a new login view.
	 *
	 * @param mainFrame the main frame
	 */
	public LoginView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	/**
	 * Layout.
	 *
	 * @return the content panel
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected JPanel layout() {
		
		JPanel panel = new JPanel(
				new GridLayout(0, ACTIVITY_COLS, H_GAP, V_GAP));
		mainPanel = new JPanel(new BorderLayout());
		scrollPane = new JScrollPane();
		scrollPane.setViewportView(panel);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_SPEED);
		
		setAvatarVisible(false);
		setParentsButtonVisible(true);

		panel.setBackground(Color.WHITE);

		ArrayList<Child> children = getMainFrame().getController(LoginController.class).getAllChildren();
		
		for(Child child : children) {
			JButton b = new JButton();
			String name = child.getName();
			b.setText(name);
			b.setFont(Theme.CHILD_FONT);
			b.setIcon(provider.getScaledImage(child.getImage()
					.getFileName(), AVATAR_SCALE));
			b.setVerticalTextPosition(SwingConstants.BOTTOM);
			b.setHorizontalTextPosition(SwingConstants.CENTER);
			b.addActionListener(this);
			if(child.isLocked()== true){
				b.setText(name.concat(" - LOCKED"));
				b.setEnabled(false);
			}
			panel.add(b);
		}
		
		if (children.size() < MIN_BUTTONS) {
			for (int i = 0; i < MIN_BUTTONS - children.size(); i++) {
				JButton b = new JButton();
				b.setVisible(false);
				panel.add(b);
			}
		}

		mainPanel.add(scrollPane, BorderLayout.CENTER);
		
		return mainPanel;
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() instanceof JButton) {
			JButton button = (JButton) e.getSource();
			getMainFrame().getController(LoginController.class).login(
					button.getText());
		}
	}

	/* (non-Javadoc)
	 * @see edu.bu.cs673.superkids123.AbstractNavigationView#getTitle()
	 */
	@Override
	public String getTitle() {
		return "Welcome to Super Kids 123 Adventure!";
	}

	/* (non-Javadoc)
	 * @see edu.bu.cs673.superkids123.AbstractNavigationView#getBorderTitle()
	 */
	@Override
	public String getBorderTitle() {
		return "Login";
	}

	/* (non-Javadoc)
	 * @see edu.bu.cs673.superkids123.AbstractNavigationView#initializeView()
	 */
	protected AbstractView<? extends JComponent> initializeView() {
		// If app is not initialized
		if (!authController.areParentsInitialized()) {
			super.initializeView();
			return getMainFrame().getView(CreateParentView.class);
		} else if (!authController.areChildrenInitialized()) {
			super.initializeView();
			return getMainFrame().getView(CreateChildView.class);
		} else if (authController.getCurrentUser() != null) {
			// If confirm logout fails don't change view
			if (confirmLogout()) {
				authController.logout();
			} else {
				return null;
			}
		}

		// Fall back is to return this view
		super.initializeView();
		return this;
	}

	/**
	 * Confirm logout.
	 *
	 * @return true, if successful
	 */
	private boolean confirmLogout() {
		int n = JOptionPane.showConfirmDialog(null,
				"Are you sure you want to logout the current user?",
				"Logout User", JOptionPane.YES_NO_OPTION);
		if (n == JOptionPane.YES_OPTION) {
			return true;
		}
		return false;
	}
}
